package com.sgb.kjwl.viewmodel.manage;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.UrlConfig;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.website.WebsiteUpDataJingyingYejiEntity;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteUpDataJingYingYeJiActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class WebsiteUpDataJingYingYeJiModel extends BaseViewModel {


    private WebsiteUpDataJingYingYeJiActivity activity;
    private ManageRepertory repertory;

    public ObservableField<WebsiteUpDataJingyingYejiEntity> entity = new ObservableField<>(new WebsiteUpDataJingyingYejiEntity());

    public WebsiteUpDataJingYingYeJiModel(WebsiteUpDataJingYingYeJiActivity activity, ManageRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /************************************************************************************
     * 获取详情
     * */
    public void updateWebsiteJYData(String id){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getYJDetail(WebsiteInfoAPI.NetBuilderUrl.YJ_DETAIL,id).enqueue(new ICallBack<BaseEntity<WebsiteUpDataJingyingYejiEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteUpDataJingyingYejiEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK))
                    activity.setYJDetail(data.getData());
            }
        });
    }

    /************************************************************************************
     * 上传数据
     * */
    public void updateWebsiteJYData(){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().updateWebsiteJYData(UrlConfig.NEW_BASE_URL+"v2/app/site/performanceCertificateAdmin/saveOrUpdate",entity.get()).enqueue(new ICallBack<BaseEntity<WebsiteUpDataJingyingYejiEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteUpDataJingyingYejiEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK))
                    activity.updateJYDataResult(TextUtils.equals(data.getCode(), "200"));
            }
        });
    }

    /************************************************************************************
     * 业绩类型
     * */
    public void getYejiType() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity pickSelectEntity1 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity2 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity3 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity4 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity5 = new PickSelectEntity();
        pickSelectEntity1.setProjectscale("工程项目");
        pickSelectEntity1.setNo("0");
        pickSelectEntity2.setProjectscale("商品供应");
        pickSelectEntity2.setNo("1");
        pickSelectEntity3.setProjectscale("租赁业绩");
        pickSelectEntity3.setNo("2");
        pickSelectEntity4.setProjectscale("物流业绩");
        pickSelectEntity4.setNo("3");
        pickSelectEntity5.setProjectscale("其他业绩");
        pickSelectEntity5.setNo("4");

        list.add(pickSelectEntity1);
        list.add(pickSelectEntity2);
        list.add(pickSelectEntity3);
        list.add(pickSelectEntity4);
        list.add(pickSelectEntity5);
        activity.setYejiTypePop(list);
    }

    /************************************************************************************
     * 项目状态
     * */
    public void getStatus() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity pickSelectEntity1 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity2 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity3 = new PickSelectEntity();
        pickSelectEntity1.setProjectscale("未开始");
        pickSelectEntity1.setNo("0");
        pickSelectEntity2.setProjectscale("进行中");
        pickSelectEntity2.setNo("1");
        pickSelectEntity3.setProjectscale("已完工");
        pickSelectEntity3.setNo("2");
        list.add(pickSelectEntity1);
        list.add(pickSelectEntity2);
        list.add(pickSelectEntity3);
        activity.setStatusPop(list);
    }

    /************************************************************************************
     * 省市区
     * */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK))
                    activity.showAddressPop(data.getData());
            }
        });
    }

    // -----上传相册照片---------------------------------------------------------------------------
    public void uploadAvatarFileModel(File file, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "头像上传中" + "...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getState() == null) {
                    MToast.showToast(activity, "上传失败！");
                    DialogHelper.dismissProgressDialog();
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.uploadImageSuccess(data.getData().toString());
                } else {
                    MToast.showToast(activity, "上传失败！");
                    DialogHelper.dismissProgressDialog();
                }
            }
        });
    }
}
